/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.ncsu.csc;

import com.sun.spot.sensorboard.EDemoBoard;
import com.sun.spot.sensorboard.peripheral.ITriColorLED;
import com.sun.spot.sensorboard.peripheral.LEDColor;

import com.sun.spot.util.*;
/**
 *
 * @author Richard Pospesel
 */
public class LEDHelper
{
	private static ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs();

	public static void setColor(int led, LEDColor color)
	{
		leds[led].setColor(color);
	}

	public static void setColor(int led, int red, int green, int blue)
	{
		leds[led].setRGB(red, green, blue);
	}

	public static void setColorAll(LEDColor color)
	{
			for(int k = 0; k < leds.length; k++)
				leds[k].setColor(color);
	}

	public static void setColorAll(int red, int green, int blue)
	{
		for(int k = 0; k < leds.length; k++)
			leds[k].setRGB(red, green, blue);
	}

	public static void enable_byte_binary(byte input)
	{
		for(int k = 0; k < 8; k++)
		{
			if(input / (128 >> k) != 0)
				enable(k);
			else
				disable(k);
			input %= (128 >> k);
		}
	}

	public static void enable(int led)
	{
		leds[led].setOn();
	}

	public static void enableAll()
	{
		for(int k = 0; k < leds.length; k++)
			leds[k].setOn();
	}

	public static void disable(int led)
	{
		leds[led].setOff();
	}

	public static void disableAll()
	{
		for(int k = 0; k < leds.length; k++)
			leds[k].setOff();
	}

	public static void flash(int time_on)
	{
		enableAll();
		Utils.sleep(time_on);
		disableAll();
	}

	public static void blink(int time_on, int time_off, int blinks)
	{
		if(blinks > 0)
		{
			for(int k = 0; k < blinks; k++)
			{
				enableAll();
				Utils.sleep(time_on);
				disableAll();
				Utils.sleep(time_off);
			}
		}
		else
		{
			while(true)
			{
				enableAll();
				Utils.sleep(time_on);
				disableAll();
				Utils.sleep(time_off);
			}
		}
	}

	public static void strobeRight(int total_time)
	{
		disableAll();
		int led_time = total_time/leds.length;
		for(int k = 0; k < leds.length; k++)
		{
			enable(k);
			Utils.sleep(led_time);
			disable(k);
		}
	}

	public static void strobeLeft(int total_time)
	{
		disableAll();
		int led_time = total_time/leds.length;
		for(int k = leds.length - 1; k > -1; k--)
		{
			enable(k);
			Utils.sleep(led_time);
			disable(k);
		}
	}
}
